<?php


namespace App\Http\Request;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class CreateArticleRequest extends FormRequest {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title'=>'required|unique:posts,title',
            'body'=>'required',
//          'userId'=>'required',

        ];
    }

    public function messages()
    {
        return [
            'title.required'=>'标题不能为空',
            'title.unique'=>'此标题已存在',
            'body.required'=>'文章内容不能为空',
            'userId.required'=>'用户ID不能为空',
            'columnId.required'=>'专栏ID不能为空',
            'columnId.in'=>'错误的专栏ID'
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'code' => '0',
            $validator->errors(),

        ], 422)->setEncodingOptions(JSON_UNESCAPED_UNICODE)));

    }

}